package com.oet.webservice.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.apache.openjpa.lib.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Document;

import com.oet.persistence.entity.Category;
import com.oet.persistence.entity.Course;
import com.oet.persistence.entity.CourseFile;
import com.oet.persistence.entity.CourseNews;
import com.oet.persistence.entity.CourseSeries;
import com.oet.persistence.entity.Customer;
import com.oet.persistence.entity.Teacher;
import com.oet.persistence.entity.UploadedFile;
import com.oet.persistence.entity.UserComment;
import com.oet.persistence.entity.UserCourseSeries;
import com.oet.persistence.entity.UserHistoryCourse;
import com.oet.persistence.entity.UserPlannedCourse;
import com.oet.persistence.manager.OetDbMngr;
import com.oet.service.manager.FileManager;
import com.oet.webservice.basic.OperationStatus;
import com.oet.webservice.basic.SimWebListResult;
import com.oet.webservice.basic.WebListResultHelper;
import com.oet.webservice.basic.WebServiceSimListParams;
import com.oet.webservice.business.data.SimWebCategoryListResult;
import com.oet.webservice.business.data.SimWebCourseFileListResult;
import com.oet.webservice.business.data.SimWebCourseListResult;
import com.oet.webservice.business.data.SimWebCourseNewsListResult;
import com.oet.webservice.business.data.SimWebCourseSeriesListResult;
import com.oet.webservice.business.data.SimWebCustomerListResult;
import com.oet.webservice.business.data.SimWebServiceFile;
import com.oet.webservice.business.data.SimWebTeacherListResult;
import com.oet.webservice.business.data.SimWebUserCommentListResult;
import com.oet.webservice.business.data.SimWebUserCourseSeriesListResult;
import com.oet.webservice.business.data.SimWebUserHistoryCourseListResult;
import com.oet.webservice.business.data.SimWebUserPlannedCourseListResult;
import com.oet.webservice.business.data.WebServiceCategory;
import com.oet.webservice.business.data.WebServiceCourse;
import com.oet.webservice.business.data.WebServiceCourseFile;
import com.oet.webservice.business.data.WebServiceCourseNews;
import com.oet.webservice.business.data.WebServiceCourseSeries;
import com.oet.webservice.business.data.WebServiceCustomer;
import com.oet.webservice.business.data.WebServiceTeacher;
import com.oet.webservice.business.data.WebServiceUserComment;
import com.oet.webservice.business.data.WebServiceUserCourseSeries;
import com.oet.webservice.business.data.WebServiceUserHistoryCourse;
import com.oet.webservice.business.data.WebServiceUserPlannedCourse;
import com.oet.webservice.business.data.WebserviceSoapHelper;


@WebService(endpointInterface = "com.oet.webservice.business.OetSimBusinessMngrService")
@Transactional
public final class OetSimBusinessMngrServiceImpl implements OetSimBusinessMngrService{

	private final Logger logger = Logger.getLogger(OetSimBusinessMngrServiceImpl.class.getName());
	protected final OetDbMngr oetDbMngr;
	
	@Autowired
	public OetSimBusinessMngrServiceImpl(OetDbMngr oetDbMngr) {
		this.oetDbMngr = oetDbMngr;
	}
	
	
	public SimWebServiceFile getImageByName(String fileName) {
		// TODO Auto-generated method stub
		SimWebServiceFile result = new SimWebServiceFile();
		if (fileName != null && !fileName.isEmpty())
		{
			//Check the fileName is valid;
			String checkResult = FileManager.checkFileName(fileName);
			if (checkResult != null)
			{
				logger.error("fileName error : " + checkResult);
				result.getOs().setIsSuccessful(false);
				result.getOs().setFailureReason(checkResult);
				return result;
			}
			
			result = FileManager.loadSimWebServiceFileFile(fileName);
			
			return result;
		}
		logger.error("Empty fileName");
		result.getOs().setIsSuccessful(false);
		result.getOs().setFailureReason("Empty fileName");
		return result;
	}
	
	public WebServiceCategory getCategoryById(Long id) {
		if (id == null)
		{
			return null;
		}
		
		Category instance = this.oetDbMngr.getCategory(id);
		
		return WebserviceSoapHelper.prepareWebCategory(instance);
	}
	
	public WebServiceCategory getCategoryByTitle(String name) {
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		Category instance = this.oetDbMngr.getCategoryByTitle(name);
		
		return WebserviceSoapHelper.prepareWebCategory(instance);
	}
	
	public OperationStatus storeCategory(WebServiceCategory instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		Category result = null;
//		Category result = oetDbMngr.getCategoryByTitle(instance.getTitle());
//		if (result != null)
//		{
//			os.setIsSuccessful(false);
//			os.setFailureReason("Duplicated title: " + instance.getTitle());
//			return os;
//		}
		
		
		
		result = new Category();
		result = WebserviceSoapHelper.prepareCategory(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}

		this.oetDbMngr.storeCategory(result);
		return os;
	}
	
	public OperationStatus updateCategory(WebServiceCategory instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		Category result = oetDbMngr.getCategory(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 Category id: " + instance.getId());
			return os;
		}
		
		
		
//		String imageOld = instance.getImage();
//		String imageIpadOld = instance.getImageIpad();
//		
//		if (imageOld != null && !imageOld.isEmpty())
//		{
//			FileManager.deleteFileByName(imageOld);
//		}
//		
//		if (imageIpadOld != null && !imageIpadOld.isEmpty())
//		{
//			FileManager.deleteFileByName(imageIpadOld);
//		}
//
//		//Here we need to store the file/images to specific path
//		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}
		
		result = WebserviceSoapHelper.prepareCategory(result, instance);
		
		this.oetDbMngr.updateCategory(result);
		
		return os;
	}
	
	public OperationStatus deleteCategory(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			Category instance = oetDbMngr.getCategory(id);
			if (instance == null)
			{
				continue;
			}
			
			List<CourseSeries> instances = oetDbMngr.searchCourseSeries(
					null, instance, null, null, null, null, null, null, null, null, null);
			if (instances != null && instances.size() > 0)
			{
				os.setIsSuccessful(false);
				os.setFailureReason("Please delete Child CourseSeries first!");
				return os;
			}
			
			
		}
		
		return os;
	}
	
	public OperationStatus addChildrenToCategory(List<Long> ids, Long channelId)
	{
		OperationStatus os = new OperationStatus();
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
//		Category result = oetDbMngr.getCategory(channelId);
//		if (result == null)
//		{
//			os.setIsSuccessful(false);
//			os.setFailureReason("未找到 Category id: " + channelId);
//			return os;
//		}
//		
//		result.getResourceIdList().addAll(ids);
//		
//		this.oetDbMngr.updateCategory(result);
		return os;
	}
	
	public OperationStatus removeChildrenFromCategory(List<Long> ids, Long channelId)
	{
		OperationStatus os = new OperationStatus();
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
//		Category result = oetDbMngr.getCategory(channelId);
//		if (result == null)
//		{
//			os.setIsSuccessful(false);
//			os.setFailureReason("未找到 Category id: " + channelId);
//			return os;
//		}
//		
//		result.getResourceIdList().removeAll(ids);
//		
//		this.oetDbMngr.updateCategory(result);
		return os;
	}
	
	
	
	public SimWebCategoryListResult seachCategory(String name, 
												WebServiceSimListParams listParams)
	{
		SimWebCategoryListResult result = new SimWebCategoryListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		
		
		list = this.oetDbMngr.searchCategory(name);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				Category instance = (Category)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebCategory(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	
	public WebServiceCourseSeries getCourseSeriesById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		CourseSeries instance = this.oetDbMngr.getCourseSeries(id);
		
		return WebserviceSoapHelper.prepareWebCourseSeries(instance);
	}

	public WebServiceCourseSeries getCourseSeriesByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		CourseSeries instance = this.oetDbMngr.getCourseSeriesByTitle(name);
		
		return WebserviceSoapHelper.prepareWebCourseSeries(instance);
	}

	public OperationStatus storeCourseSeries(WebServiceCourseSeries instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		//We need to do something for double
		if (instance.getScore() != null && instance.getScore().isNaN())
		{
			instance.setScore(null);
		}
		if (instance.getTotalPrice() != null && instance.getTotalPrice().isNaN())
		{
			instance.setTotalPrice(null);
		}
		
//		CourseSeries result = null;
		CourseSeries result = oetDbMngr.getCourseSeriesByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		
		
		result = new CourseSeries();
		result = WebserviceSoapHelper.prepareCourseSeries(result, instance);
		
		//Now we need to check the teacher Id
		if (instance.getTeacherId() <= 0)
		{
			instance.setTeacherId(null);
			instance.setTeacherName(null);
		}
		else
		{
			Teacher teacher = oetDbMngr.getTeacher(instance.getTeacherId());
			result.setTeacher(teacher);
		}
		
		if (instance.getWebServiceCategory() != null && instance.getWebServiceCategory().getId() != null)
		{
			Category category = oetDbMngr.getCategory(instance.getWebServiceCategory().getId());
			if (category != null)
				result.setCategory(category);
		}
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}
		
		

		this.oetDbMngr.storeCourseSeries(result);
		return os;
	}
	
	public OperationStatus updateCourseSeries(WebServiceCourseSeries instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		CourseSeries result = oetDbMngr.getCourseSeries(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 CourseSeries id: " + instance.getId());
			return os;
		}
		
		if (instance.getScore() != null && instance.getScore().isNaN())
		{
			instance.setScore(null);
		}
		if (instance.getTotalPrice() != null && instance.getTotalPrice().isNaN())
		{
			instance.setTotalPrice(null);
		}
		
		result = WebserviceSoapHelper.prepareCourseSeries(result, instance);
		
		if (instance.getWebServiceCategory() != null && instance.getWebServiceCategory().getId() != null)
		{
			Category category = oetDbMngr.getCategory(instance.getWebServiceCategory().getId());
			if (category != null)
				result.setCategory(category);
		}
		
		//Now we need to check the teacher Id
		if (instance.getTeacherId() <= 0)
		{
			instance.setTeacherId(null);
			instance.setTeacherName(null);
		}
		else
		{
			Teacher teacher = oetDbMngr.getTeacher(instance.getTeacherId());
			result.setTeacher(teacher);
		}
		
		this.oetDbMngr.updateCourseSeries(result);
		
		return os;
	}

	public OperationStatus deleteCourseSeries(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			CourseSeries instance = oetDbMngr.getCourseSeries(id);
			if (instance == null)
			{
				continue;
			}
			
			// We need to delete all the courses in this CourseSeries
			for (Course temp : instance.getCourses())
			{
				oetDbMngr.deleteCourse(temp.getId());
			}
			
			oetDbMngr.deleteCourseSeries(id);
			
			
		}
		
		return os;
	}

	public SimWebCourseSeriesListResult seachCourseSeries(
			String name, Long categoryId, Integer level, Integer courseNumber,
			Long teacherId, Integer capacity, Integer occupyNum, 
			Date startDate, Date endDate, Boolean isActived, Double totalPrice,
			WebServiceSimListParams listParams)
	{
		SimWebCourseSeriesListResult result = new SimWebCourseSeriesListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		
		//We need to the the validity of Integer and Long
		if (categoryId == -1) categoryId = null;
		if (level == -1) level = null;
		if (courseNumber == -1) courseNumber = null;
		if (teacherId == -1) teacherId = null;
		if (capacity == -1) capacity = null;
		if (occupyNum == -1) occupyNum = null;
		if (totalPrice == -1) totalPrice = null;
		
		//We need to get the Entity of some Id
		Category category = null;
		if (categoryId != null)
		{
			category = oetDbMngr.getCategory(categoryId);
		}
		
		Teacher teacher = null;
		if (teacherId != null)
		{
			teacher = oetDbMngr.getTeacher(teacherId);
		}
		
		list = this.oetDbMngr.searchCourseSeries(name, category, 
				level, courseNumber, teacher,
				capacity, occupyNum, startDate, endDate, 
				isActived, totalPrice);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				CourseSeries instance = (CourseSeries)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebCourseSeries(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	public WebServiceCourseFile getCourseFileById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		CourseFile instance = this.oetDbMngr.getCourseFile(id);
		
		return WebserviceSoapHelper.prepareWebCourseFile(instance);
	}

	public WebServiceCourseFile getCourseFileByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		CourseFile instance = this.oetDbMngr.getCourseFileByTitle(name);
		
		return WebserviceSoapHelper.prepareWebCourseFile(instance);
	}

	public OperationStatus storeCourseFile(WebServiceCourseFile instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		CourseFile result = null;
		CourseFile result = oetDbMngr.getCourseFileByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		
		
		result = new CourseFile();
		result = WebserviceSoapHelper.prepareCourseFile(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_COURSE_FILE);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setFileName(fileName);
		}
		

		this.oetDbMngr.storeCourseFile(result);
		return os;
	}
	
	public OperationStatus updateCourseFile(WebServiceCourseFile instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		CourseFile result = oetDbMngr.getCourseFile(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 CourseFile id: " + instance.getId());
			return os;
		}
		
		//First we need to remember the old file to delete;
		String oldFileName = result.getFileName();
		
		
		result = WebserviceSoapHelper.prepareCourseFile(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_COURSE_FILE);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setFileName(fileName);
		}
		
		this.oetDbMngr.updateCourseFile(result);
		
		//OK, now we need to delete the old file.
		//OK, Now we need to Delete the images files and zip files of the old epubbook;
		if (oldFileName != null && !oldFileName.isEmpty())
		{
			FileManager.deleteFileByName(oldFileName);
		}
		
		return os;
	}

	public OperationStatus deleteCourseFile(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			oetDbMngr.deleteCourseFile(id);
			
		}
		
		return os;
	}

	public SimWebCourseFileListResult seachCourseFile(
			Long courseId, Long courseSeriesId, String name, Integer type,
			Integer version, Date startDate, Date endDate, WebServiceSimListParams listParams)
	{
		SimWebCourseFileListResult result = new SimWebCourseFileListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		
		
		//We need to the the validity of Integer and Long
		if (courseId == -1) courseId = null;
		if (courseSeriesId == -1) courseSeriesId = null;
		if (type == -1) type = null;
		if (version == -1) version = null;

		
		list = this.oetDbMngr.searchCourseFile(courseId, courseSeriesId, name, type,
				version, startDate, endDate);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				CourseFile instance = (CourseFile)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebCourseFile(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	public WebServiceCourseNews getCourseNewsById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		CourseNews instance = this.oetDbMngr.getCourseNews(id);
		
		return WebserviceSoapHelper.prepareWebCourseNews(instance);
	}

	public WebServiceCourseNews getCourseNewsByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		CourseNews instance = this.oetDbMngr.getCourseNewsByTitle(name);
		
		return WebserviceSoapHelper.prepareWebCourseNews(instance);
	}

	public OperationStatus storeCourseNews(WebServiceCourseNews instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		CourseNews result = null;
		CourseNews result = oetDbMngr.getCourseNewsByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		
		
		result = new CourseNews();
		result = WebserviceSoapHelper.prepareCourseNews(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}

		this.oetDbMngr.storeCourseNews(result);
		return os;
	}
	
	public OperationStatus updateCourseNews(WebServiceCourseNews instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		CourseNews result = oetDbMngr.getCourseNews(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 CourseNews id: " + instance.getId());
			return os;
		}
		
		
		
		result = WebserviceSoapHelper.prepareCourseNews(result, instance);
		
		this.oetDbMngr.updateCourseNews(result);
		
		return os;
	}

	public OperationStatus deleteCourseNews(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			oetDbMngr.deleteCourseNews(id);
			
		}
		
		return os;
	}

	public SimWebCourseNewsListResult seachCourseNews(
			Long courseId, Long courseSeriesId, String name, Integer type,
			Integer version, Date startDate, Date endDate,WebServiceSimListParams listParams)
	{
		SimWebCourseNewsListResult result = new SimWebCourseNewsListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		//We need to the the validity of Integer and Long
		if (courseId == -1) courseId = null;
		if (courseSeriesId == -1) courseSeriesId = null;
		if (type == -1) type = null;
		if (version == -1) version = null;
		
		list = this.oetDbMngr.searchCourseNews(courseId, courseSeriesId, name, type,
				version, startDate, endDate);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				CourseNews instance = (CourseNews)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebCourseNews(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	public WebServiceCustomer getCustomerById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		Customer instance = this.oetDbMngr.getCustomer(id);
		
		return WebserviceSoapHelper.prepareWebCustomer(instance);
	}

	public WebServiceCustomer getCustomerByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		Customer instance = this.oetDbMngr.getCustomerByTitle(name);
		
		return WebserviceSoapHelper.prepareWebCustomer(instance);
	}

	public OperationStatus storeCustomer(WebServiceCustomer instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		Customer result = null;
		Customer result = oetDbMngr.getCustomerByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		
		
		result = new Customer();
		result = WebserviceSoapHelper.prepareCustomer(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CUSTOMER);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setThumbnail(fileName);
		}

		this.oetDbMngr.storeCustomer(result);
		return os;
	}
	
	public OperationStatus updateCustomer(WebServiceCustomer instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		Customer result = oetDbMngr.getCustomer(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 Customer id: " + instance.getId());
			return os;
		}
		
		//First we need to remember the old file to delete;
		String oldFileName = result.getThumbnail();
		
		result = WebserviceSoapHelper.prepareCustomer(result, instance);
		
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CUSTOMER);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setThumbnail(fileName);
		}
		
		this.oetDbMngr.updateCustomer(result);
		
		//OK, now we need to delete the old file.
		//OK, Now we need to Delete the images files and zip files of the old epubbook;
		if (oldFileName != null && !oldFileName.isEmpty())
		{
			FileManager.deleteFileByName(oldFileName);
		}
		
		
		
		return os;
	}

	public OperationStatus deleteCustomer(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		os.setIsSuccessful(false);
		os.setFailureReason("Currently delete customer is not allowed!");
		return os;
		/*
		for (Long id: ids)
		{
			Customer instance = oetDbMngr.getCustomer(id);
			if (instance == null)
			{
				continue;
			}
			
			List<Customer> instances = oetDbMngr.searchCustomer(null,
					null, null, instance.getId());
			if (instances != null && instances.size() > 0)
			{
				os.setIsSuccessful(false);
				os.setFailureReason("Please delete Child Customers first!");
				return os;
			}
			
			
		}
		
		return os;
		*/
	}

	public SimWebCustomerListResult seachCustomer(String name, String email, String mobilePhone, 
			Integer level, Integer couresHours, Boolean isActived, Integer totalCourseNumber,
			Boolean isTestFinished, Long accountId,WebServiceSimListParams listParams)
	{
		SimWebCustomerListResult result = new SimWebCustomerListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		//We need to the the validity of Integer and Long
		if (level == -1) level = null;
		if (couresHours == -1) couresHours = null;
		if (totalCourseNumber == -1) totalCourseNumber = null;
		if (accountId == -1) accountId = null;
		
		list = this.oetDbMngr.searchCustomer(name, email, mobilePhone, 
				level, couresHours, isActived, totalCourseNumber,
				isTestFinished, accountId);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				Customer instance = (Customer)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebCustomer(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	public WebServiceTeacher getTeacherById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		Teacher instance = this.oetDbMngr.getTeacher(id);
		
		return WebserviceSoapHelper.prepareWebTeacher(instance);
	}

	public WebServiceTeacher getTeacherByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		Teacher instance = this.oetDbMngr.getTeacherByTitle(name);
		
		return WebserviceSoapHelper.prepareWebTeacher(instance);
	}

	public OperationStatus storeTeacher(WebServiceTeacher instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		Teacher result = null;
		Teacher result = oetDbMngr.getTeacherByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		if (instance.getScore() != null && instance.getScore().isNaN())
		{
			instance.setScore(null);
		}
		
		
		
		result = new Teacher();
		result = WebserviceSoapHelper.prepareTeacher(result, instance);
		
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_TEACHER);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setThumbnail(fileName);
		}

		this.oetDbMngr.storeTeacher(result);
		return os;
	}
	
	public OperationStatus updateTeacher(WebServiceTeacher instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		Teacher result = oetDbMngr.getTeacher(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 Teacher id: " + instance.getId());
			return os;
		}
		

		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		if (instance.getScore() != null && instance.getScore().isNaN())
		{
			instance.setScore(null);
		}
		//First we need to remember the old file to delete;
		String oldFileName = result.getThumbnail();
		
		
		result = WebserviceSoapHelper.prepareTeacher(result, instance);
		
		
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_TEACHER);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setThumbnail(fileName);
		}
		
		this.oetDbMngr.updateTeacher(result);
		
		//OK, now we need to delete the old file.
		//OK, Now we need to Delete the images files and zip files of the old epubbook;
		if (oldFileName != null && !oldFileName.isEmpty())
		{
			FileManager.deleteFileByName(oldFileName);
		}
		
		return os;
	}

	public OperationStatus deleteTeacher(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			Teacher instance = oetDbMngr.getTeacher(id);
			if (instance == null)
			{
				continue;
			}
			
			List<CourseSeries> instances = oetDbMngr.searchCourseSeries(
					null, null, null, null, instance, null, null, null, null, null, null);
			if (instances != null && instances.size() > 0)
			{
				os.setIsSuccessful(false);
				os.setFailureReason("Please delete Teacher's CourseSeries first!");
				return os;
			}
			
			oetDbMngr.deleteTeacher(id);
			
		}
		
		return os;
	}

	public SimWebTeacherListResult seachTeacher(String name, String email, String mobilePhone, 
			Integer level, Boolean isActived, Integer totalCourseNumber,
			WebServiceSimListParams listParams)
	{
		SimWebTeacherListResult result = new SimWebTeacherListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		//We need to the the validity of Integer and Long
		if (level == -1) level = null;
		if (totalCourseNumber == -1) totalCourseNumber = null;
		
		list = this.oetDbMngr.searchTeacher(name, email, mobilePhone, 
				level, isActived, totalCourseNumber);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				Teacher instance = (Teacher)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebTeacher(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	public WebServiceCourse getCourseById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		Course instance = this.oetDbMngr.getCourse(id);
		
		return WebserviceSoapHelper.prepareWebCourse(instance);
	}

	public WebServiceCourse getCourseByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		Course instance = this.oetDbMngr.getCourseByTitle(name);
		
		return WebserviceSoapHelper.prepareWebCourse(instance);
	}

	public OperationStatus storeCourse(WebServiceCourse instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		if (instance.getCourseSeriesId() < 0)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("CourseSeries Id could not be invalid number");
			return os;
		}
		
		CourseSeries parent = oetDbMngr.getCourseSeries(instance.getCourseSeriesId());
		if (parent == null || parent.getId() == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Can not find CourseSeries by CourseSeries Id: " + instance.getCourseSeriesId());
			return os;
		}
		
//		Course result = null;
		Course result = oetDbMngr.getCourseByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		

		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		if (instance.getScore() != null && instance.getScore().isNaN())
		{
			instance.setScore(null);
		}
		if (instance.getDurationFactor() != null && instance.getDurationFactor().isNaN())
		{
			instance.setDurationFactor(null);
		}
		
		
		result = new Course();
		result = WebserviceSoapHelper.prepareCourse(result, instance);
		result.setCourseSeries(parent);
		
		
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_COURSE);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setTopicPic(fileName);
		}

		this.oetDbMngr.storeCourse(result);
		
		parent.setCourseNumber(parent.getCourseNumber() + 1);
		this.oetDbMngr.storeCourseSeries(parent);
		return os;
	}
	
	public OperationStatus updateCourse(WebServiceCourse instance, 
			byte[] content)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		Course result = oetDbMngr.getCourse(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 Course id: " + instance.getId());
			return os;
		}
		
		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		if (instance.getScore() != null && instance.getScore().isNaN())
		{
			instance.setScore(null);
		}
		if (instance.getDurationFactor() != null && instance.getDurationFactor().isNaN())
		{
			instance.setDurationFactor(null);
		}
		
		//First we need to remember the old file to delete;
		String oldFileName = result.getTopicPic();
		
		result = WebserviceSoapHelper.prepareCourse(result, instance);
		
		
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_COURSE);
		if (fileName == null)
		{
			logger.error("Could not generate File name");

			os.setIsSuccessful(false);
			os.setFailureReason("Could not generate File name");
			return os;
		}
		
		
		if (content != null && content.length > 0)
		{
			//Store the file;
			FileManager.saveAsFile(fileName , content);
			result.setTopicPic(fileName);
		}
		
		this.oetDbMngr.updateCourse(result);
		
		//OK, now we need to delete the old file.
		//OK, Now we need to Delete the images files and zip files of the old epubbook;
		if (oldFileName != null && !oldFileName.isEmpty())
		{
			FileManager.deleteFileByName(oldFileName);
		}
		
		return os;
	}

	public OperationStatus deleteCourse(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		int i=0;
		Long courseSeriesId = null;
		Course course = null;
		for (Long id: ids)
		{
			course = oetDbMngr.getCourse(id);
			if ( course!= null && course.getId() == id)
			{
				i++;
				courseSeriesId = course.getCourseSeries().getId();
			}
			oetDbMngr.deleteCourse(id);
		}
		
		if (i > 0 && courseSeriesId != null)
		{
			CourseSeries courseSeries = oetDbMngr.getCourseSeries(courseSeriesId);
			courseSeries.setCourseNumber(courseSeries.getCourses().size());
			oetDbMngr.updateCourseSeries(courseSeries);
		}
		
		return os;
	}

	public SimWebCourseListResult seachCourse(Long courseSeriesId,
			String name,  Integer chapterId, Integer type, 
			Integer level, Double payFactor, Long teacherId,
			String topic, Integer capacity, Integer occupyNum, Date startDate, Date endDate,
			WebServiceSimListParams listParams)
	{
		SimWebCourseListResult result = new SimWebCourseListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		
		

		//We need to the the validity of Integer and Long
		if (courseSeriesId == -1) courseSeriesId = null;
		if (level == -1) level = null;
		if (chapterId == -1) chapterId = null;
		if (teacherId == -1) teacherId = null;
		if (capacity == -1) capacity = null;
		if (occupyNum == -1) occupyNum = null;
		if (type == -1) type = null;
		if (payFactor == -1) payFactor = null;
		
		//We need to get the Entity of some Id
		CourseSeries courseSeries = null;
		if (courseSeriesId != null)
		{
			courseSeries = oetDbMngr.getCourseSeries(courseSeriesId);
		}
		
//		Teacher teacher = null;
//		if (teacherId != null)
//		{
//			teacher = oetDbMngr.getTeacher(teacherId);
//		}
		
		list = this.oetDbMngr.searchCourse(courseSeries,
				 name,   chapterId,  type, 
				 level,  payFactor,  teacherId,
				 topic,  capacity,  occupyNum,  startDate,  endDate);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				Course instance = (Course)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebCourse(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	public WebServiceUserComment getUserCommentById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		UserComment instance = this.oetDbMngr.getUserComment(id);
		
		return WebserviceSoapHelper.prepareWebUserComment(instance);
	}

	public WebServiceUserComment getUserCommentByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		UserComment instance = this.oetDbMngr.getUserCommentByTitle(name);
		
		return WebserviceSoapHelper.prepareWebUserComment(instance);
	}

	public OperationStatus storeUserComment(WebServiceUserComment instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getTopic() == null
			|| instance.getTopic().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		UserComment result = null;
		UserComment result = oetDbMngr.getUserCommentByTitle(instance.getTopic());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getTopic());
			return os;
		}
		
		
		
		result = new UserComment();
		result = WebserviceSoapHelper.prepareUserComment(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}

		this.oetDbMngr.storeUserComment(result);
		return os;
	}
	
	public OperationStatus updateUserComment(WebServiceUserComment instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getTopic() == null
			|| instance.getTopic().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		UserComment result = oetDbMngr.getUserComment(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 UserComment id: " + instance.getId());
			return os;
		}
		
		
		
		result = WebserviceSoapHelper.prepareUserComment(result, instance);
		
		this.oetDbMngr.updateUserComment(result);
		
		return os;
	}

	public OperationStatus deleteUserComment(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			oetDbMngr.deleteUserComment(id);
			
		}
		
		return os;
	}

	public SimWebUserCommentListResult seachUserComment(
			Long courseId, Long courseSeriesId, Long teacherId, 
			String topic, Integer type, Integer level, 
			Date startDate, Date endDate, Long customerId,
			WebServiceSimListParams listParams)
	{
		SimWebUserCommentListResult result = new SimWebUserCommentListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		

		//We need to the the validity of Integer and Long
		if (courseId == -1) courseId = null;
		if (courseSeriesId == -1) courseSeriesId = null;
		if (teacherId == -1) teacherId = null;
		if (type == -1) type = null;
		if (level == -1) level = null;
		if (customerId == -1) customerId = null;
		
		//We need to get the Entity of some Id
		Customer customer = null;
		if (customerId != null)
		{
			customer = oetDbMngr.getCustomer(customerId);
		}
		
		
		list = this.oetDbMngr.searchUserComment(
				courseId, courseSeriesId, teacherId, 
				topic, type, level, 
				startDate, endDate, customer);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				UserComment instance = (UserComment)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebUserComment(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	public WebServiceUserCourseSeries getUserCourseSeriesById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		UserCourseSeries instance = this.oetDbMngr.getUserCourseSeries(id);
		
		return WebserviceSoapHelper.prepareWebUserCourseSeries(instance);
	}

	public WebServiceUserCourseSeries getUserCourseSeriesByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		UserCourseSeries instance = this.oetDbMngr.getUserCourseSeriesByTitle(name);
		
		return WebserviceSoapHelper.prepareWebUserCourseSeries(instance);
	}

	public OperationStatus storeUserCourseSeries(WebServiceUserCourseSeries instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		UserCourseSeries result = null;
		UserCourseSeries result = oetDbMngr.getUserCourseSeriesByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		if (instance.getTotalPrice() != null && instance.getTotalPrice().isNaN())
		{
			instance.setTotalPrice(null);
		}
		
		
		
		
		result = new UserCourseSeries();
		result = WebserviceSoapHelper.prepareUserCourseSeries(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}

		this.oetDbMngr.storeUserCourseSeries(result);
		return os;
	}
	
	public OperationStatus updateUserCourseSeries(WebServiceUserCourseSeries instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		UserCourseSeries result = oetDbMngr.getUserCourseSeries(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 UserCourseSeries id: " + instance.getId());
			return os;
		}
		
		if (instance.getTotalPrice() != null && instance.getTotalPrice().isNaN())
		{
			instance.setTotalPrice(null);
		}
		
		result = WebserviceSoapHelper.prepareUserCourseSeries(result, instance);
		
		this.oetDbMngr.updateUserCourseSeries(result);
		
		return os;
	}

	public OperationStatus deleteUserCourseSeries(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			oetDbMngr.deleteUserCourseSeries(id);
			
		}
		
		return os;
	}

	public SimWebUserCourseSeriesListResult seachUserCourseSeries(
			Long courseSeriesId, String name,  
			Integer type, Integer level, Long teacherId, String teacherName,Integer capacity,
			Date startDate, Date endDate, Long customerId,WebServiceSimListParams listParams)
	{
		SimWebUserCourseSeriesListResult result = new SimWebUserCourseSeriesListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		//We need to the the validity of Integer and Long
		if (courseSeriesId == -1) courseSeriesId = null;
		if (teacherId == -1) teacherId = null;
		if (type == -1) type = null;
		if (level == -1) level = null;
		if (customerId == -1) customerId = null;
		
		if (capacity == -1) capacity = null;
		
		//We need to get the Entity of some Id
		Customer customer = null;
		if (customerId != null)
		{
			customer = oetDbMngr.getCustomer(customerId);
		}
		
		list = this.oetDbMngr.searchUserCourseSeries(
				courseSeriesId, name,  
				type, level, teacherId, teacherName,capacity,
				startDate, endDate, customer);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				UserCourseSeries instance = (UserCourseSeries)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebUserCourseSeries(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	
	
	
	public WebServiceUserHistoryCourse getUserHistoryCourseById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		UserHistoryCourse instance = this.oetDbMngr.getUserHistoryCourse(id);
		
		return WebserviceSoapHelper.prepareWebUserHistoryCourse(instance);
	}

	public WebServiceUserHistoryCourse getUserHistoryCourseByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		UserHistoryCourse instance = this.oetDbMngr.getUserHistoryCourseByTitle(name);
		
		return WebserviceSoapHelper.prepareWebUserHistoryCourse(instance);
	}

	public OperationStatus storeUserHistoryCourse(WebServiceUserHistoryCourse instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		UserHistoryCourse result = null;
		UserHistoryCourse result = oetDbMngr.getUserHistoryCourseByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		
		if (instance.getDurationFactor() != null && instance.getDurationFactor().isNaN())
		{
			instance.setDurationFactor(null);
		}
		
		result = new UserHistoryCourse();
		result = WebserviceSoapHelper.prepareUserHistoryCourse(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}

		this.oetDbMngr.storeUserHistoryCourse(result);
		return os;
	}
	
	public OperationStatus updateUserHistoryCourse(WebServiceUserHistoryCourse instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		UserHistoryCourse result = oetDbMngr.getUserHistoryCourse(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 UserHistoryCourse id: " + instance.getId());
			return os;
		}
		
		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		
		if (instance.getDurationFactor() != null && instance.getDurationFactor().isNaN())
		{
			instance.setDurationFactor(null);
		}
		
		result = WebserviceSoapHelper.prepareUserHistoryCourse(result, instance);
		
		this.oetDbMngr.updateUserHistoryCourse(result);
		
		return os;
	}

	public OperationStatus deleteUserHistoryCourse(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			oetDbMngr.deleteUserHistoryCourse(id);
			
		}
		
		return os;
	}

	public SimWebUserHistoryCourseListResult seachUserHistoryCourse(
			Long courseId, Long courseSeriesId, String name,  
			Integer type, Integer level, Double payFactor, Long teacherId, String teacherName,
			String topic, Integer capacity,
			Date startDate, Date endDate, Boolean isCommented, Long customerId,WebServiceSimListParams listParams)
	{
		SimWebUserHistoryCourseListResult result = new SimWebUserHistoryCourseListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		
		//We need to the the validity of Integer and Long
		if (courseId == -1) courseId = null;
		if (courseSeriesId == -1) courseSeriesId = null;
		if (teacherId == -1) teacherId = null;
		if (type == -1) type = null;
		if (level == -1) level = null;
		if (payFactor == -1) payFactor = null;
		if (customerId == -1) customerId = null;
		
		if (capacity == -1) capacity = null;
		
		//We need to get the Entity of some Id
		Customer customer = null;
		if (customerId != null)
		{
			customer = oetDbMngr.getCustomer(customerId);
		}
		
		list = this.oetDbMngr.searchUserHistoryCourse(
				courseId, courseSeriesId, name,  
				type, level, payFactor, teacherId, teacherName,
				topic, capacity,
				startDate, endDate, isCommented,customer);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				UserHistoryCourse instance = (UserHistoryCourse)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebUserHistoryCourse(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	public WebServiceUserPlannedCourse getUserPlannedCourseById(Long id)
	{
		if (id == null)
		{
			return null;
		}
		
		UserPlannedCourse instance = this.oetDbMngr.getUserPlannedCourse(id);
		
		return WebserviceSoapHelper.prepareWebUserPlannedCourse(instance);
	}

	public WebServiceUserPlannedCourse getUserPlannedCourseByTitle(String name)
	{
		if (name == null || name.isEmpty())
		{
			return null;
		}
		
		UserPlannedCourse instance = this.oetDbMngr.getUserPlannedCourseByTitle(name);
		
		return WebserviceSoapHelper.prepareWebUserPlannedCourse(instance);
	}

	public OperationStatus storeUserPlannedCourse(WebServiceUserPlannedCourse instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
//		UserPlannedCourse result = null;
		UserPlannedCourse result = oetDbMngr.getUserPlannedCourseByTitle(instance.getName());
		if (result != null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Duplicated title: " + instance.getName());
			return os;
		}
		
		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		
		if (instance.getDurationFactor() != null && instance.getDurationFactor().isNaN())
		{
			instance.setDurationFactor(null);
		}
		
		
		result = new UserPlannedCourse();
		result = WebserviceSoapHelper.prepareUserPlannedCourse(result, instance);
		
		//Here we need to store the file/images to specific path
		//and add the fileDescriptor to instance.
//		String fileName = FileManager.generateFileName(FileManager.FILE_KEY_CHANNEL);
//		if (fileName == null)
//		{
//			logger.error("Could not generate File name");
//
//			os.setIsSuccessful(false);
//			os.setFailureReason("Could not generate File name");
//			return os;
//		}
//		
//		if (image != null && image.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName , image);
//			result.setImage(fileName);
//		}
//		
//		if (imageIpad != null && imageIpad.length > 0)
//		{
//			//Store the file;
//			FileManager.saveAsFile(fileName + "ipad", imageIpad);
//			result.setImageIpad(fileName + "ipad");
//		}

		this.oetDbMngr.storeUserPlannedCourse(result);
		return os;
	}
	
	public OperationStatus updateUserPlannedCourse(WebServiceUserPlannedCourse instance)
	{
		OperationStatus os = new OperationStatus();
		if (instance == null 
			|| instance.getId() == null
			|| instance.getName() == null
			|| instance.getName().isEmpty())
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input");
			return os;
		}
		
		UserPlannedCourse result = oetDbMngr.getUserPlannedCourse(instance.getId());
		if (result == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("未找到 UserPlannedCourse id: " + instance.getId());
			return os;
		}
		
		if (instance.getPayFactor() != null && instance.getPayFactor().isNaN())
		{
			instance.setPayFactor(null);
		}
		
		if (instance.getDurationFactor() != null && instance.getDurationFactor().isNaN())
		{
			instance.setDurationFactor(null);
		}
		
		result = WebserviceSoapHelper.prepareUserPlannedCourse(result, instance);
		
		this.oetDbMngr.updateUserPlannedCourse(result);
		
		return os;
	}

	public OperationStatus deleteUserPlannedCourse(List<Long> ids)
	{
		OperationStatus os = new OperationStatus();		
		if (ids == null)
		{
			os.setIsSuccessful(false);
			os.setFailureReason("Null input ids");
			return os;
		}
		
		for (Long id: ids)
		{
			oetDbMngr.deleteUserPlannedCourse(id);
			
		}
		
		return os;
	}

	public SimWebUserPlannedCourseListResult seachUserPlannedCourse(
			Long courseId, Long courseSeriesId, String name,  
			Integer type, Integer level, Double payFactor, Long teacherId, String teacherName,
			String topic, Integer capacity,
			Date startDate, Date endDate, Long customerId, WebServiceSimListParams listParams)
	{
		SimWebUserPlannedCourseListResult result = new SimWebUserPlannedCourseListResult();
		if (listParams == null)
		{
			result.getOs().setIsSuccessful(false);
			result.getOs().setFailureReason("Null input");
			return result;
		}
		
		List list = new ArrayList(0);
		//We need to the the validity of Integer and Long
		if (courseId == -1) courseId = null;
		if (courseSeriesId == -1) courseSeriesId = null;
		if (teacherId == -1) teacherId = null;
		if (type == -1) type = null;
		if (level == -1) level = null;
		if (payFactor == -1) payFactor = null;
		if (customerId == -1) customerId = null;
		
		if (capacity == -1) capacity = null;
		
		//We need to get the Entity of some Id
		Customer customer = null;
		if (customerId != null)
		{
			customer = oetDbMngr.getCustomer(customerId);
		}
		
		list = this.oetDbMngr.searchUserPlannedCourse(
				courseId, courseSeriesId, name,  
				type, level, payFactor, teacherId, teacherName,
				topic, capacity,
				startDate, endDate, customer);
		
		SimWebListResult toolList = new SimWebListResult();
		toolList = (SimWebListResult) WebListResultHelper.getSpecifiedPageList(list, listParams, toolList);
		
		if (toolList.getList() != null)
		{
			for(int i = 0; i < toolList.getList().size(); i++)
			{
				UserPlannedCourse instance = (UserPlannedCourse)toolList.getList().get(i);
				result.getList().add(WebserviceSoapHelper.prepareWebUserPlannedCourse(instance));
			}
			
			result.setTotalItems(toolList.getTotalItems());
			result.setTotalPages(toolList.getTotalPages());
			result.setCurrentPage(toolList.getCurrentPage());
		}

		return result;
	}
	
	

}
